//
//  NotifyListController.m
//  SelfDriving
//
//  Created by apple on 16/7/9.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "NotifyListController.h"
#import "MsgCell.h"
#import "XWNewMsgCell.h"
#import "XhwWebViewController.h"
#import "DetailsViewController.h"
#import "KPDViewController.h"
#import "BusinessService+FJ.h"
@interface NotifyListController ()<UITableViewDelegate,UITableViewDataSource,BaseTableDetelage,UIActionSheetDelegate,TCResultNoneDelegate>
@property (weak, nonatomic) IBOutlet BaseTable *table;
@property (nonatomic, strong) TCNotify*       actionNotify;


@end

@implementation NotifyListController{
    NSMutableArray *contentArr;
    UIActionSheet *sheet;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    contentArr = [[NSMutableArray alloc]init];
    _table.bdelegate = self;
    [_table registerNib:[UINib nibWithNibName:@"XWNewMsgCell" bundle:nil] forCellReuseIdentifier:XWNewMsgCellID];
    
    //xhw2018-1-30加
    UIButton* ButSign = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
    
    [ButSign setTitle:@"清空" forState:UIControlStateNormal];

    
    ButSign.titleLabel.font = [UIFont systemFontOfSize:15.0];
    
    [ButSign setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [ButSign setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    
    [ButSign addTarget:self action:@selector(rightBarItemClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:ButSign];
}

- (void)rightBarItemClick {
    NSArray *notifyArr = [[ThinkChat instance] getNotifyListTimeLineWithFinalNotify:nil];
    for (TCNotify *notify in notifyArr) {
        switch (notify.type) {
            case 211:
                
                break;
            case 209:
                break;
            case 300:
                break;
            case 302:
                break;
            case 304:
                break;
            case 1002:
                break;
            case 1003:
                break;
            case 1004:
                break;
            case 1006:
                break;
            case 1007:
                break;
            case 1008:
                break;
            case 1009:
                break;
            case 1011:
                break;
            case 1012:  //同一wifi
                break;
            case 1013:  //附近快拍
                break;
            case 1014:  //我的快拍
                break;
            default:
                [TCNotify hasDeleteNotify:notify];
                break;
        }
    }
    [self initData:1];
}
-(void)viewWillAppear:(BOOL)animated{
  
    [_table Refresh];
}
-(void)initData:(int)index{
    
    if (index !=0) {
//        TCNotify* itemM = [contentArr lastObject];  //xhw2018-2-23注释
        
//        NSArray* arr = [[ThinkChat instance] getNotifyListTimeLineWithFinalNotify:itemM]; //获取所有的（包括不显示的）
        [contentArr removeAllObjects];
        NSArray* arr = [[ThinkChat instance] getNotifyListTimeLineWithFinalNotify:nil];
        for (TCNotify* notify in arr) {
            //211 209 300 、302、304 、1002、1003、1004、1006、1007、1008、1009、1011
            switch (notify.type) {
                case 211:
                    break;
                case 209:
                    break;
                case 300:
                    break;
                case 302:
                    break;
                case 304:
                    break;
                case 1002:
                    break;
                case 1003:
                    break;
                case 1004:
                    break;
                case 1006:
                    break;
                case 1007:
                    break;
                case 1008:
                    break;
                case 1009:
                    break;
                case 1011:
                    break;
                case 1012:  //同一wifi
                    break;
                case 1013:  //附近快拍
                    break;
                case 1014:  //我的快拍
                    break;
                default:
                    [contentArr addObject:notify];
                    break;
            }
        }
//        [contentArr addObjectsFromArray:arr];
    }else{
        [contentArr removeAllObjects];
        NSArray* arr = [[ThinkChat instance] getNotifyListTimeLineWithFinalNotify:nil];
        for (TCNotify* notify in arr) {
            //211 209 300 、302、304 、1002、1003、1004、1006、1007、1008、1009、1011
            switch (notify.type) {
                case 211:
                    break;
                case 209:
                    break;
                case 300:
                    break;
                case 302:
                    break;
                case 304:
                    break;
                case 1002:
                    break;
                case 1003:
                    break;
                case 1004:
                    break;
                case 1006:
                    break;
                case 1007:
                    break;
                case 1008:
                    break;
                case 1009:
                    break;
                case 1011:
                    break;
                case 1012:  //同一wifi
                    break;
                case 1013:  //附近快拍
                    break;
                case 1014:  //我的快拍
                    break;
                default:
                    [contentArr addObject:notify];
                    break;
            }
        }
//        [contentArr addObjectsFromArray:[[ThinkChat instance] getNotifyListTimeLineWithFinalNotify:nil]];
    }
    NSLog(@"xhw->系统通知->initData->contentArr.count:%lu",(unsigned long)contentArr.count);
    [_table reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return contentArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 79;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TCNotify *msg = [contentArr objectAtIndex:indexPath.row];
 
        XWNewMsgCell *cell = [tableView dequeueReusableCellWithIdentifier:XWNewMsgCellID];
    
        cell.icon.layer.cornerRadius = cell.icon.frame.size.width/2;
    
        [[AppDelegate shareDelegate]showMsgCount];
    
        cell.icon.image = [UIImage imageNamed:@"msgxt"];
    
        cell.name.text = @"系统通知";
    
        NSString *time = [ValidateHelper timeStringWith:msg.time multiLine:NO];
    
        if (msg.type == forNotifyType_inKuaipai) {//进入快拍
    
            cell.contentlable.text = [NSString stringWithFormat:@"%@进入快拍",msg.user.ID];
    
    
        }
        //加了下面两个esle if（xhw11月2号改）
        else if (msg.type == forNotifyType_fujingkuaipai) {
            cell.contentlable.text = [NSString stringWithFormat:@"%@进入快拍",msg.user.ID];
        }
        else if (msg.type == forNotifyType_wodekuaipai) {
            cell.contentlable.text = [NSString stringWithFormat:@"%@进入快拍",msg.user.ID];
        }
        //加了下面的esle if（xhw11月28号改）
        else if (msg.type == forNotifyType_tongyiwifi) {
            cell.contentlable.text = [NSString stringWithFormat:@"%@进入快拍",msg.user.ID];
        }
        else if (msg.type == forNotifyType_zanKuaipai) {//进入快拍
            cell.contentlable.text = [NSString stringWithFormat:@"%@赞了你",msg.user.ID];
    
    
        }else if(msg.type == forNotifyType_ConversationRemove) {
            cell.contentlable.text = [NSString stringWithFormat:@"您被管理员提出[%@]群",msg.group.name];
    
    
        }
    //xhw2018-1-19加
        else if (msg.type == forNotifyType_pushMsg) {
            NSDictionary * dic = [ValidateHelper jsonStringToDictionary:msg.content];
            cell.name.text = dic[@"title"];
            cell.contentlable.text = dic[@"url"];
        }
        //xhw2018-1-30加
        else if (msg.type == forNotifyType_NEWMSGinKuaipai) {
            cell.contentlable.text = @"有人评论了你的动态";
        }
        else if (msg.type == forNotifyType_praiseDynamic) {
            cell.contentlable.text = @"有人赞了你的动态";
        }
        else if (msg.type == forNotifyType_friendSendDynamic) {
            cell.contentlable.text = @"有好友发布了新的动态";
        }
    
        else{
    
            cell.contentlable.text = msg.content;
    
        }
    
    
//        cell.time.text = time;
    
        cell.numberBgn.layer.cornerRadius = cell.numberBgn.frame.size.width/2;
        cell.numberBgn.clipsToBounds = YES;
        cell.numberBgn.hidden = YES;
        if (!msg.isRead) {
             cell.numberBgn.hidden = NO;
            [cell.numberBgn setTitle:[NSString stringWithFormat:@"%d",1] forState:UIControlStateNormal];
        }else{
            cell.numberBgn.hidden = YES;
        }

    return  cell;
}
#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)sender didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [sender deselectRowAtIndexPath:indexPath animated:YES];
    _actionNotify = [contentArr objectAtIndex:indexPath.row];
    [[ThinkChat instance] hasReadNotify:_actionNotify];
    if (_actionNotify.isDone) {
        return;
    }
    switch (_actionNotify.type) {
        case forNotifyType_System:
            // 1   系统通知
            
            break;
        case forNotifyType_ConversationQuit:
            // 300 用户退出 会话
            
            break;
        case forNotifyType_ConversationRemove:
            // 301 管理员删除 会话用户
            
            break;
        case forNotifyType_ConversationEdit:
            // 302 管理员编辑 会话名称
            
            break;
        case forNotifyType_ConversationDele:
            // 303 管理员删除 会话
            
            break;
        case forNotifyType_ConversationKick:
            // 304 自己被踢出 会话
            
            break;
        case forNotifyType_GroupJoinApply:
            // 202 申请加 群
            sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"同意",@"拒绝",@"用户详细", nil];
            
            break;
        case forNotifyType_GroupJoinAgree:
            // 203 同意申请入 群
            
            break;
        case forNotifyType_GroupJoinRefuse:
            // 204 不同意申请入 群
            
            break;
        case forNotifyType_GroupInviteApply:
            // 205 邀请入 群
            sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"同意",@"拒绝",@"群组详细", nil];
            break;
        case forNotifyType_GroupInviteAgree:
            // 206 同意邀请入 群
            
            break;
        case forNotifyType_GroupInviteRefuse:
            // 207 不同意邀请入 群
            
            break;
        case forNotifyType_GroupKick:
            // 208 自己被踢出 群
            
            break;
        case forNotifyType_GroupQuit:
            // 209 成员退出 群
            
            break;
        case forNotifyType_GroupDele:
            // 210 管理员删除 群
            
            break;
        case forNotifyType_GroupRemove:
            // 211 管理员删除 群成员
            
            break;
        //添加后台推送新闻消息
        case forNotifyType_pushMsg:
            {
                XhwWebViewController * web = [[XhwWebViewController alloc]init];
                self.hidesBottomBarWhenPushed = YES;
                NSDictionary * dic = [ValidateHelper jsonStringToDictionary:_actionNotify.content];
                web.urlString = dic[@"url"];
                web.ADid = dic[@"id"];
                [self.navigationController pushViewController:web animated:YES];
                self.hidesBottomBarWhenPushed = NO;
            break;
            }
        case forNotifyType_NEWMSGinKuaipai :
        {
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"Dynaminc"
                                                                         bundle:[NSBundle mainBundle]];
            DetailsViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"DetailsVC"];
            NSLog(@"xhw->notifyListController->点击了一个cell->content%@",_actionNotify.content);
            NSMutableDictionary * contentDic = [JsonService jsonObjectWithString:_actionNotify.content];
            NSLog(@"xhw->notifyListController->点击了一个cell->content%@",contentDic);
            
            searchResultDetailController.dynamicID = contentDic[@"id"];
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
//            NSString* iskuaipai = contentDic[@"iskuaipai"];
//            NSString* kid = contentDic[@"iskuaipai"];
//            if ([iskuaipai isEqualToString:@"1"]) { //是快拍
//                NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
//                [dic setObject:kid forKey:@"id"];
//                [SVProgressHUD showWithStatus:@"加载中..."];
//                [[BusinessService shareInstance]kuaipai_detail:dic success:^(id result) {
//                    LoginResponseModel *user = result;
//                    [[BusinessService shareInstance]enterGroup:dic success:^(id result) {
//                        [SVProgressHUD dismiss];
//                        LoginResponseModel *models = result;
//                        UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"FJ"
//                                                                                     bundle:[NSBundle mainBundle]];
//                        KPDViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"KPDViewController"];
//                        searchResultDetailController.model = models;
//                        searchResultDetailController.user = user;
//                        searchResultDetailController.modelId = kid;
//
//                        [self.navigationController pushViewController:searchResultDetailController animated:YES];
//
//
//
//
//                    } failure:^(id result) {
//                        [SVProgressHUD showErrorWithStatus:result];
//                    }];
//                } failure:^(id result) {
//                    [SVProgressHUD showErrorWithStatus:result];
//                }];
//            }
//            else {  //不是快拍
//
//            }
            break;
        }
        case forNotifyType_praiseDynamic:
        {
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"Dynaminc"
                                                                         bundle:[NSBundle mainBundle]];
            DetailsViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"DetailsVC"];
            NSLog(@"xhw->notifyListController->点击了一个cell->content%@",_actionNotify.content);
            NSMutableDictionary * contentDic = [JsonService jsonObjectWithString:_actionNotify.content];
            NSLog(@"xhw->notifyListController->点击了一个cell->content%@",contentDic);
            
            searchResultDetailController.dynamicID = contentDic[@"id"];
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
            
//            NSString* iskuaipai = contentDic[@"iskuaipai"];
//            if ([iskuaipai isEqualToString:@"1"]) { //是快拍
//
//            }
//            else {  //不是快拍
//
//            }
            break;
        }
        case forNotifyType_friendSendDynamic:
        {
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"Dynaminc"
                                                                         bundle:[NSBundle mainBundle]];
            DetailsViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"DetailsVC"];
            NSLog(@"xhw->notifyListController->点击了一个cell->content%@",_actionNotify.content);
            NSMutableDictionary * contentDic = [JsonService jsonObjectWithString:_actionNotify.content];
            NSLog(@"xhw->notifyListController->点击了一个cell->content%@",contentDic);
            
            searchResultDetailController.dynamicID = contentDic[@"id"];
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
            
//            NSString* iskuaipai = contentDic[@"iskuaipai"];
//            if ([iskuaipai isEqualToString:@"1"]) { //是快拍
//
//            }
//            else {  //不是快拍
//
//            }
            break;
        }
        default:
            break;
    }
    [sheet  showFromTabBar:self.tabBarController.tabBar];
    [sender reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    
}
#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)sender didDismissWithButtonIndex:(NSInteger)buttonIndex {
    NSString* strTitle = [sender buttonTitleAtIndex:buttonIndex];
    if ([strTitle isEqualToString:@"取消"]) {
        return;
    }
    if ([strTitle isEqualToString:@"同意"]) {
        [[ThinkChat instance] agreeApplyJoinGroup:_actionNotify.group.ID user:_actionNotify.user.ID delegate:self];
    } else if ([strTitle isEqualToString:@"拒绝"]) {
        [[ThinkChat instance] refuseApplyJoinGroup:_actionNotify.group.ID user:_actionNotify.user.ID delegate:self];
    } else if ([strTitle isEqualToString:@"用户详细"]) {
        LoginResponseModel *user = [[LoginResponseModel alloc]init];
        user.uid = _actionNotify.user.ID;
 
        
    } else if ([strTitle isEqualToString:@"群组详细"]) {
        LoginResponseModel *user = [[LoginResponseModel alloc]init];
        user.id = _actionNotify.group.ID;
      
        
        
    }
    
}
#pragma mark - TCResultNoneDelegate

- (void)tcResultNoneError:(TCError *)itemE {
    if (itemE == nil) {
        [[ThinkChat instance] hasDoneNotify:_actionNotify];
        [[ThinkChat instance] hasReadNotify:_actionNotify];        
        [SVProgressHUD showSuccessWithStatus:@"操作成功"];
    } else {
        [SVProgressHUD showErrorWithStatus:itemE.message];
    }
}

-(NSString*)tableView:(UITableView *)sender titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

//设置滑动，
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView *)sender commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        TCNotify* itemN = [contentArr objectAtIndex:indexPath.row];
        [[ThinkChat instance] hasDeleteNotify:itemN];
        [contentArr removeObject:itemN];
        [_table reloadData];
    }
}
- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


@end
